package com.rkexample.cam.service;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;



import java.util.Timer;
import java.util.TimerTask;

public class NetSpeedService extends Service {
    public static final String SEND_ACTION="SEND_ACTION";
    private Timer mGetSpeedTimer;
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        stopSpeedTimer();
        startSpeedTimer();
        return START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSpeedTimer();
    }
    private void startSpeedTimer() {
        if(mGetSpeedTimer==null){
            mGetSpeedTimer = new Timer();
            TimerTask mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    Intent sendIntent = new Intent(SEND_ACTION);
                    getApplicationContext().sendBroadcast(sendIntent);
               //     Log.e("YYY", "网络测试定时器  ");
                }
            };
            mGetSpeedTimer.scheduleAtFixedRate(mTimerTask,0,1000);
        }
    }

    private void stopSpeedTimer() {
        if (mGetSpeedTimer != null) {
            mGetSpeedTimer.cancel();
            mGetSpeedTimer = null;
        }
    }
}